﻿/**
 * Copyright (C) 2012-2013 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Spi;
using NGuice.Inject.Internal.Util;
using NGuice.Inject.Util;

namespace NGuice.Inject.Internal
{
    //complete!
    public class InstanceBindingImpl<T> : BindingImpl<T>, IInstanceBinding<T>
    {
        readonly T instance;
        readonly IProvider<T> provider;
        readonly ImmutableSet<InjectionPoint> injectionPoints;

        public InstanceBindingImpl(InjectorImpl injector, Key<T> key, object source,
                                    InternalFactory<T> internalFactory, ISet<InjectionPoint> injectionPoints, T instance)
            : base(injector, key, source, internalFactory, Scoping.EAGER_SINGLETON)
        {
            this.injectionPoints = ImmutableSet<InjectionPoint>.CopyOf<InjectionPoint, InjectionPoint>(injectionPoints);
            this.instance = instance;
            this.provider = Providers.Of(instance);
        }

        public InstanceBindingImpl(object source, Key<T> key, Scoping scoping, ISet<InjectionPoint> injectionPoints, T instance)
            : base(source, key, scoping)
        {
            this.injectionPoints = ImmutableSet<InjectionPoint>.CopyOf<InjectionPoint, InjectionPoint>(injectionPoints);
            this.instance = instance;
            this.provider = Providers.Of(instance);
        }

        public override IProvider<T> Provider
        {
            get
            {
                return this.provider;
            }
        }

        public override V AcceptTargetVisitor<V>(IBindingTargetVisitor<T, V> visitor)
        {
            return visitor.Visit((IInstanceBinding<T>)this);
        }

        public T Instance
        {
            get { return instance; }
        }

        public ISet<InjectionPoint> InjectionPoints
        {
            get { return injectionPoints; }
        }

        public ISet<Dependency> Dependencies
        {
            get
            {
                return (instance is IHasDependencies)
                    ? ImmutableSet<Dependency>.CopyOf<Dependency, Dependency>(((IHasDependencies)instance).Dependencies)
                    : Dependency<T>.ForInjectionPoints(injectionPoints);
            }
        }

        protected internal override BindingImpl<T> WithScoping(Scoping scoping)
        {
            return new InstanceBindingImpl<T>(Source, Key, scoping, injectionPoints, instance);
        }

        protected internal override BindingImpl<T> WithKey(Key<T> key)
        {
            return new InstanceBindingImpl<T>(Source, key, Scoping, injectionPoints, instance);
        }

        public override void ApplyTo(IBinder binder)
        {
            // instance bindings aren't scoped
            binder.WithSource(Source).Bind(Key).ToInstance(instance);
        }

        public override string ToString()
        {
            return new ToStringBuilder(typeof(IInstanceBinding<T>))
                .Add("key", Key)
                .Add("source", Source)
                .Add("instance", instance)
                .ToString();
        }

        public override bool Equals(object obj)
        {
            if (obj is InstanceBindingImpl<T>)
            {
                InstanceBindingImpl<T> o = (InstanceBindingImpl<T>)obj;
                return Key.Equals(o.Key)
                  && Scoping.Equals(o.Scoping)
                  && Objects.Equal(instance, o.instance);
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return Objects.GetHashCode(Key, Scoping);
        }

    }
}
